using System.Collections;
using Curriculum.Services;
using Utiles;

namespace ConcursoCV.UI.WebUserControls.Concurso
{
	using System;

	/// <summary>
	///		Summary description for ListadoDocentes.
	/// </summary>
	public class ListadoDocentes : System.Web.UI.UserControl
	{
		protected System.Web.UI.WebControls.DataGrid dtgDocentes;
		protected System.Web.UI.WebControls.Label lblCount;
		protected System.Web.UI.WebControls.DataGrid dtgPostulantes;

		private const string pidCVs = "010";

		private void Page_Load(object sender, System.EventArgs e)
		{
			ChequearPermisos();
			if(!this.IsPostBack)
			{
				InitializeVariables();
				FillDataGridWithSearch();
			}
		}

		private void ChequearPermisos()
		{
//			if( Helper.SecurityHelper.EsAdminitrador() || Helper.SecurityHelper.EsRoleGenerico() )
//			{
//				dtgDocentes.Columns[4].Visible = false;
//			}
//			if( Helper.SecurityHelper.EsJurado() )
//			{
//				dtgDocentes.Columns[3].Visible = false;
//			}
		}

		private void InitializeVariables()
		{
			SortColumn = "Nombre";
			SortDirection = true;			
		}

		private IList GetListBySearch() 
		{
			// Search			
			DocenteService service = new DocenteService();			
			return service.ObtenerDocentesConCV();
		}
		private void FillDataGridWithSearch()
		{
			IList list = GetListBySearch();
			((ArrayList)list).Sort(new GenericComparer(SortColumn,SortDirection));
			dtgDocentes.DataSource = list;
			lblCount.Text = list.Count.ToString();
			try
			{
				dtgDocentes.DataBind();
			}
			catch(Exception exc )
			{
				string msg = exc.Message;
				dtgDocentes.CurrentPageIndex = 0;
				FillDataGridWithSearch();
			}

			list = null;
		}

		private void RedirigirADetalleCV(string docenteId)
		{			
			string url = "Default.aspx?pid=" + pidCVs + "&docenteId=" + docenteId  + "&type=CONSULTA";
			Server.Transfer(url);
		}
		
		private void Redirigir(string pid)
		{
			string url = "Default.aspx?pid=" + pid;
			Server.Transfer(url);
		}

		#region - properties -		

		#region - Sort Variables -

		public string SortColumn
		{
			get{ return (string)ViewState["SortColumn"];}
			set{ ViewState["SortColumn"] = value;}
		}
		public bool SortDirection
		{
			get{ return (bool)ViewState["SortDirection"];}
			set{ ViewState["SortDirection"] = value;}
		}

		#endregion - Sort Variables -

		#endregion - properties -

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);

			this.dtgDocentes.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dtgDocentes_ItemCommand);
			this.dtgDocentes.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dtgDocentes_PageIndexChanged);
			this.dtgDocentes.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.dtgDocentes_SortCommand);
					

		}
		#endregion

		private void dtgDocentes_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			if ( e.CommandName != "Page" )
			{
				string docenteId = e.Item.Cells[0].Text;
				switch ( e.CommandName )
				{
					case "Ver":
						RedirigirADetalleCV(docenteId);
						break;					
				}
			}				
		}

		private void dtgDocentes_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
		{
			dtgDocentes.CurrentPageIndex = e.NewPageIndex;
			FillDataGridWithSearch();
		}

		private void dtgDocentes_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
		{
			//Si se hace click sobre ,la misma columna, cambia el orden
			if (SortColumn.ToString() == e.SortExpression.ToString())
				SortDirection = !SortDirection;
			
			SortColumn = e.SortExpression;
			FillDataGridWithSearch();		
		}
	}
}
